﻿using hCommand;
using Microsoft.Win32.SafeHandles;
using Net.http;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Persistence {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/

    public class PP {
        public long id1 { get; set; }
        public long id2 { get; set; }
        public double countDb { get; set; }
    }
    public class CsdUtil {



        public const String param_value_remain = "remain";

        static public void setCSDPLoc(Sw sw, long pid, long locId) {
            return ;
            String sql2 = "update Core_Stock_Det set p_loc=@locId  where pid=@pid "; 
            sw.exec(sql2, new { pid, locId });
        }


        static public  int countDet(Sw sw,long pid) {
            String sql = "select count(*) from Core_Stock_Det  where pid=@pid and status<{0}";
            sql = HUtil.s(sql, StockDetailStatus.上限);
            int remain = sw.queryUnique<int>(sql, new { pid});
            return remain;
        }

        static public void updateCSdetCount(Sw sw, long pid) {
            int remain = CsdUtil.countDet(sw, pid);
            if (remain == 0) {
                sw.exec("update Core_Stock set classify_Id=0,det_count=0 where id=@pid  ", new { pid });
            } else {
                sw.exec("update Core_Stock set  det_count=@remain where id=@pid  ", new { remain,pid });
            }
        }


        static public CoreStockDet split(Sw sw, CoreStockDet old, double nowOutCount) {
            if (same(nowOutCount, old.countDb)) {
                return old;
            }

            if (nowOutCount < old.countDb) {

                CoreStockDet model = (CoreStockDet)old.Clone();
                model.id = 0;
                model.countDb = old.countDb - nowOutCount;

                sw.insert(model);

                old.countDb = nowOutCount;
                string sql = "update  core_stock_det set count_Db=@countDb where id=@Id";
                sw.exec(sql, old);
                updateCSdetCount(sw, old.pid);
            }
            return old;
        }

        static public void MarginDet(Sw sw, long pid ) {
            String sql1 = @"SELECT d1.id  id1, d2.id id2 , d2.count_db  FROM `core_stock_det` d1 join `core_stock_det` 
        d2  where   d1.pid=d2.pid and d1.item_Id=d2.item_Id and d1.`status`=d2.`status` and d1.unit=d2.unit
        and d1.create_Time=d2.create_Time and d1.doi_det_id=d2.doi_det_id and d1.doi_type=d2.doi_type and d1.bar_Code=d2.bar_Code 
        and d1.batch_No=d2.batch_No  and d1.pid=@pid";
            List<PP> pps = sw.query<PP>(sql1, new { pid });
            if (pps.Count > 0) { 
                sw.exec("update  core_stock_det set count_Db=count_Db+@countDb where id=@Id1", pps[0]);
                sw.exec("delete  core_stock_det   where id=@Id2", pps[0]);
                updateCSdetCount(sw, pid);
            }
             
          
        }

        static public bool same(double v1, double v2) {
            return Math.Abs(v1 - v2) < loss;
        }


        static public double Round(double s) { 
        return Math.Round(s,6);
        }

        public const double loss = 0.000001;
    }
}
